﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;

public partial class Manage_Papers : ManagePageBase {

    protected List<Exam.Core.Model.Department> DeptList = new List<Exam.Core.Model.Department>();
    protected List<Exam.Core.Model.Course> CourseList = new List<Exam.Core.Model.Course>();

    protected void Page_Load(object sender, EventArgs e) {
        btnSearch.Text = Resources.labels.search;
        DeptList = new Exam.Core.DAL.Department().getDepartmentAllList();
        if(!IsPostBack){
            BindData();
            
            if (isTheMe.CourseId.Trim() != "")
            {
                CourseList = new Exam.Core.DAL.Course().getCoursesListByMasterId(isTheMe.MasterID);
                CourseList.Sort();
            }
        }
    }

    private void BindData() {
        string starTime = this.starTime.Text.Trim();
        string endTime = this.endTime.Text.Trim();
        string keyword = this.keywordTb.Text.Trim();

        int pagesize = AspNetPager1.PageSize;
        int page = AspNetPager1.CurrentPageIndex - 1; //GetPageIndex();
        int size = new Exam.Core.DAL.Paper().getSize(
                isTheMe.DepartmentID.Trim(','),
                starTime,
                endTime,
                keyword);
        AspNetPager1.RecordCount = size;
        List<Exam.Core.Model.Paper> list = new Exam.Core.DAL.Paper().getPaperListByPage(
                isTheMe.DepartmentID.Trim(','),
                pagesize,
                page,
                starTime,
                endTime,
                keyword
            );
        rp.DataSource = list;
        rp.DataBind();

        Label1.Text = list.Count.ToString();
    }

    protected string getdept(string deptid)
    {
        foreach(Exam.Core.Model.Department de in DeptList){
            if (de.DepartmentID.Trim() == deptid.Trim())
            {
                return de.DepartmentName.Trim();
            }
        }
        return "";
    }


    private string _currentpaperid = Tools.ReadText(HttpContext.Current.Server.MapPath("~/App_Data/currentpaper.txt"));
    protected string getStatus(string _iscurrexam)
    {
        if (_iscurrexam.Trim() == "1")
        {
            return "<img src='../images/current.png' style='vertical-align:middle;' /> " + Resources.labels.currentExam;
        }
        return "";
    }

    protected void rp_ItemDataBound(object sender, RepeaterItemEventArgs e) {

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
            Exam.Core.Model.Paper rw = (Exam.Core.Model.Paper)e.Item.DataItem;
            List<Exam.Core.Model.PaperQuestionType> model = rw.PaperQuestionTypes;
            if (model == null)
                return;
            StringBuilder sb = new StringBuilder();
            float total = 0;
            int totalCount = 0;
            foreach (Exam.Core.Model.PaperQuestionType o in model) {
                sb.Append(o.QuestionTypeTitle.Trim() + o.QuestionCount.Trim() + "，");
                total = total + float.Parse(o.Score.Trim()) * int.Parse(o.QuestionCount.Trim());
                totalCount = totalCount + int.Parse(o.QuestionCount.Trim());
            }
            string tmp = Resources.labels.paper_qtotal_score;
            tmp = string.Format(tmp, totalCount, total);
            //sb.Append("共计" + totalCount + "道试题，");
            //sb.Append("总分" + total + "分");
            sb.Append(tmp);
            
            Literal litTj = (Literal)e.Item.FindControl("litTj");
            litTj.Text = sb.ToString();
        }

    }

    public string getDisabled(string pid) {
        string result = "";
        if (IsStarExam(pid)) {
            result = "disabled";
        }
        return result;
    }

    private bool IsStarExam(string pid) {
        return new Exam.Core.DAL.Paper().IsStarExam(pid);
    }


    protected void btnSearch_Click(object sender, EventArgs e) {
        BindData();
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e) {
        BindData();
    }

}
